import 'package:aero_frisbee/core/extension/string_ext.dart';
import 'package:aero_frisbee/core/state/view_state.dart';
import 'package:flutter/material.dart';
import 'package:flutter_smart_dialog/flutter_smart_dialog.dart';

extension ViewStateExt<T> on ViewState<T> {
  void showError() {
    final error = maybeWhen(error: ((error) => error), orElse: () => null);
    if (error != null) {
      /// 现实错误提示
      error.toast();
    }
  }

  String get error {
    final error = maybeWhen(error: ((error) => error), orElse: () => null);

    return error ?? '';
  }

  void showLoadding(
      {bool animation = true, bool isDarkMode = false, String darkMs = '加载中'}) {
    final isLoading = maybeWhen(loading: (() => true), orElse: () => false);
    if (isLoading) {
      if (isDarkMode) {
        SmartDialog.showLoading(msg: darkMs);
      } else {
        SmartDialog.showLoading(
          backDismiss: false,
          builder: (_) => const CircularProgressIndicator(),
        );
      }
    } else {
      SmartDialog.dismiss();
    }
  }
}
